package com.ctg.behavior.calc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.impala.calculator.CtFunnelCalculator;
import com.ctg.behavior.common.calc.CtCalcResult;
import com.ctg.behavior.common.funnel.CtFunnelCalcRequest;
import com.ctg.behavior.common.funnel.CtFunnelCalcResult;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api/funnels")
public class CtFunnelReportController {

	@Autowired
	private CtFunnelCalculator calculator;

//	@PostMapping("/report")
//	public CtFunnelCalcData calcReport(@RequestParam("id") Long id, @RequestParam("bookmarkId") String bookmarkId, @RequestParam("async") Boolean async,
//			@RequestParam("timeout") Integer timeout, @RequestBody CtFunnelCalcBody funnelCalcBody) throws Exception {
//
//		CtFunnelCalcData calcData = new CtFunnelCalcData();
//		log.info("id={}, bookmarkId={}, async={}, timeout={}", id, bookmarkId, async, timeout);
//		log.info("body={}", JSON.toJSONString(funnelCalcBody));
//
//		CtCalcResult result = calculator.doCalculator(funnelCalcBody);
//		log.info("calcResult={}", JSON.toJSONString(result));
//		if (!(result instanceof CtFunnelCalcResult)) {
//			log.error("calcResult class={}", result.getClass());
//			log.error("calcResult={}", JSON.toJSONString(result));
//			return calcData;
//		}
//		CtFunnelCalcResult calcResult = (CtFunnelCalcResult) result;
//		return calcResult.getValue();
//
//	}

	@PostMapping("/report")
	public CtFunnelCalcResult calcReport(@RequestBody CtFunnelCalcRequest request) throws Exception {

		CtFunnelCalcResult calcResult = new CtFunnelCalcResult();
		log.info("body={}", JSON.toJSONString(request.getReq()));

		CtCalcResult result = calculator.doCalculator(request.getReq());
		log.info("calcResult={}", JSON.toJSONString(result));
		if (!(result instanceof CtFunnelCalcResult)) {
			log.error("calcResult class={}", result.getClass());
			log.error("calcResult={}", JSON.toJSONString(result));
			return calcResult;
		}
		calcResult = (CtFunnelCalcResult) result;
		return calcResult;

	}
}
